package com.androidwwf.jwr;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WWFWidget extends AppWidgetProvider {

	String rTip;

	
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub

		super.onUpdate(context, appWidgetManager, appWidgetIds);
		getTips();
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget);
		views.setTextViewText(R.id.tvUpdateTips, rTip);
		appWidgetManager.updateAppWidget(appWidgetIds, views);
		updateWidget(context);
	}

	public void updateWidget(Context context) {
		RemoteViews updateViews = new RemoteViews(context.getPackageName(),
				R.layout.mainscreen);
		Intent intent = new Intent(context, WWFWidget.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);
		updateViews.setOnClickPendingIntent(R.id.bGames, pendingIntent);
	        updateWidget(context);
	}
	
//	public void updateWidget(Context context){
//	    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mainscreen);
//	    Intent intent = new Intent(context, WWFWidget.class);
//	    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//	    updateViews.setOnClickPendingIntent(R.id.bGames, pendingIntent);
//	}


	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
		Toast.makeText(context, "WNF widget deleted.", Toast.LENGTH_SHORT)
				.show();
	}

	public String randomGenerator(String[] array) {
		Random generator = new Random();

		int random = generator.nextInt(array.length);
		return array[random];
	}

	public String getTips() {
		String[] tips = {
				  "Tip 1: Sign the forrest petition."
				, "Tip 2: Dry your clean laundry outside."
				, "Tip 3: Check your tire pressure."
				, "Tip 4: Eat no meat once in a while."
				, "Tip 5: Give your old stuff a second life."
				, "Tip 6: Sign the Zee Vol Vis petition."
				, "Tip 7: Dive with policy."
				, "Tip 8: Insulate your home."
				, "Tip 9: Adopt an animal."
				, "Tip 10: Bank paperless"
				, "Tip 11: Choose for MSV-fish."
				, "Tip 12: Join as a WWF volunteer."
				, "Tip 13: Join in social media about the WWF."
				, "Tip 14: Switch to green power."
				, "Tip 15: Switch to electric vehicles."
				, "Tip 16: Save energy, turn more than half washes."
				, "Tip 17: Save the forrest, buy FSC-wood"
				, "Tip 18: Support WWF and become donor"
				, "Tip 19: Send an e-card more often instead of by post."
				, "Tip 20: Stick to the time in the shower."
				, "Tip 21: Turn appliances off instead of standby."
				, "Tip 22: Use the bike more frequently."
				, "Tip 23: Take your own shopping bag to the store."
				, "Tip 24: Join the CO2 reduction."
				, "Tip 25: Put a WWF gift membership."
				, "Tip 26: Eat fruits and vegetables from the season or from the garden."
				, "Tip 27: Tap water for the climate."
				, "Tip 28: Make an animal friend happy and become a member of WWF-club."
				, "Tip 29: Pick up more when walking the dog."
				, "Tip 30: Use long lasting light bulbs."	
		};
		
		rTip = randomGenerator(tips);

		return rTip;
	}
}
